package system.logger;

import java.util.ArrayList;

import com.hong.alert.dao.ReferenceDataRepossitory;

import system.main.SystemDataStructure;
import system.queues.LogQueue;
import system.wrap_filter.Standard;

public class Logger extends Thread {
	private LogQueue logQueue;
	private ReferenceDataRepossitory referenceRepository;
	private Standard standard;
	private boolean run, flag;
	private ArrayList<Standard> standardList;

	public Logger(SystemDataStructure ds) {
		this.logQueue = ds.getLogQueue();
		this.referenceRepository = ds.getReferenceDataRepossitory();
		standardList = new ArrayList<Standard>();
		run = flag = true;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (run) {
			if (logQueue.getSize() < 10 && logQueue.getSize() > 0) {
				referenceRepository.pushLog(standard);
				flag = false;
			} else if (logQueue.getSize() < 100) {
				for (int i = 0; i < 100; i++) {
					if ((standard = logQueue.poll()) != null) {
						standardList.add(standard);
					} else {
						break;
					}
				}
			} else if (logQueue.getSize() < 1000) {
				for (int i = 0; i < 999; i++) {
					if ((standard = logQueue.poll()) != null) {
						standardList.add(standard);
					} else {
						break;
					}
				}
			} else if (logQueue.getSize() < 2000) {
				for (int i = 0; i < 1999; i++) {
					if ((standard = logQueue.poll()) != null) {
						standardList.add(standard);
					} else {
						break;
					}
				}
			} else {
				for (int i = 0; i < 4999; i++) {
					if ((standard = logQueue.poll()) != null) {
						standardList.add(standard);
					} else {
						break;
					}
				}
			}
			if (flag) {
				referenceRepository.pushLog(standardList);
			}
		}
	}
}

// referenceRepository.pushLog(standard);
